package org.kgc.alanhealth.controller;

import org.kgc.alanhealth.entity.*;
import org.kgc.alanhealth.service.MyInformationService;
import org.kgc.alanhealth.service.UserService;
import org.kgc.alanhealth.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class MyInformationController {
    @Autowired
    private MyInformationService myInformationService;


    @RequestMapping("hospital-user")
    public Result getUserById(int id){
        List<HospitalUser> list=myInformationService.getUserById(id);
        if (list!=null){
            return Result.success(list);
        }else{
            return Result.error("没有找到数据");
        }
    }
    @RequestMapping("hospital-health")
    public Result getHealthById(int id) {
        List<HospitalHealthmanage> list = myInformationService.getHealthManageById(id);
        if (list != null) {
            return Result.success(list);
        } else {
            return Result.error("没有找到数据");
        }
    }
    @GetMapping("check-idcard-exists")
    public Result checkIdCardExists(@RequestParam("idcard") String idcard) {
        List<HospitalHealthmanage> list = myInformationService.getHealthManageByCard(idcard);
        if (list != null && !list.isEmpty()) {
            return Result.success(true);
        } else {
            return Result.success(false);
        }
    }

    @RequestMapping("health-allergy-all")
    public Result getAllAllergy(){
        List<HospitalAllergy> list= myInformationService.selectAllAllergy();
        if (list!= null) {
            return Result.success(list);
        } else {
            return Result.error("没有找到数据");
        }
    }
    @RequestMapping("health-allergy")
    public Result getAllergyById(int id) {
        List<HospitalAllergy> list= myInformationService.selectAllergyById(id);
        if (list!= null) {
            return Result.success(list);
        } else {
            return Result.error("没有找到数据");
        }
    }
    @RequestMapping("health-anamnesis")
    public Result getAnamnesisById(int id) {
        List<HospitalDisease> list= myInformationService.selectAnamnesisById(id);
        if (list!= null) {
            return Result.success(list);
        } else {
            return Result.error("没有找到数据");
        }
    }
    @RequestMapping("health-anamnesis-all")
    public Result getAllAnamnesis() {
        List<HospitalDisease> list= myInformationService.selectAllAnamnesis();
        if (list!= null) {
            return Result.success(list);
        } else {
            return Result.error("没有找到数据");
        }
    }

    @PostMapping("hospital-health-add")
    public Result addHealthManage(@RequestBody HospitalHealthmanage healthManage) {
        System.out.println(healthManage);
        try {
            boolean added = myInformationService.addHealthManage(healthManage);
            if (added) {
                return Result.success("健康信息添加成功");
            } else {
                return Result.error("健康信息添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器异常，添加失败");
        }
    }
    @PutMapping("hospital-health-update/{id}")
    public Result updateHealth(@PathVariable int id, @RequestBody HospitalHealthmanage healthInfo) {
        try {
            healthInfo.setId(id); // 设置实体对象的 ID
            boolean isSuccess = myInformationService.updateHealthManage(healthInfo);
            if (isSuccess) {
                return Result.success("健康信息更新成功");
            } else {
                return Result.error("健康信息更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务异常：" + e.getMessage());
        }
    }
    @DeleteMapping("hospital-health-delete/{id}")
    public Result deleteHealth(@PathVariable int id) {
        try {
            boolean isSuccess = myInformationService.deleteHealthManage(id);
            if (isSuccess) {
                return Result.success("健康信息删除成功");
            } else {
                return Result.error("健康信息删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务异常：" + e.getMessage());
        }
    }


    @RequestMapping("hospital-address")
    public Result getAddressById(int id) {
        List<HospitalUseraddress> list = myInformationService.getAddressById(id);
        if (list != null) {
            return Result.success(list);
        } else {
            return Result.error("没有找到数据");
        }
    }

    @PostMapping("hospital-address-add")
    public Result addAddressManage(@RequestBody HospitalUseraddress hospitalUseraddress) {
        System.out.println(hospitalUseraddress);
        try {
            boolean added = myInformationService.addAddress(hospitalUseraddress);
            if (added) {
                return Result.success("健康信息添加成功");
            } else {
                return Result.error("健康信息添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器异常，添加失败");
        }
    }
    @PutMapping("hospital-address-update/{id}")
    public Result updateAddress(@PathVariable int id, @RequestBody HospitalUseraddress hospitalUseraddress) {
        try {
            System.out.println(hospitalUseraddress);
            hospitalUseraddress.setId(id); // 设置实体对象的 ID
            boolean isSuccess = myInformationService.updateAddress(hospitalUseraddress);
            if (isSuccess) {
                return Result.success("健康信息更新成功");
            } else {
                return Result.error("健康信息更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务异常：" + e.getMessage());
        }
    }
    @DeleteMapping("hospital-address-delete/{id}")
    public Result deleteAddress(@PathVariable int id) {
        try {
            boolean isSuccess = myInformationService.deleteAddressManage(id);
            if (isSuccess) {
                return Result.success("健康信息删除成功");
            } else {
                return Result.error("健康信息删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务异常：" + e.getMessage());
        }
    }



}
